package cn.monkey.family.data.uc.resource;

import cn.monkey.family.data.EntityMapper;
import cn.monkey.family.data.Identifiable;
import cn.monkey.family.data.TreeMapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.util.Collection;

@Mapper(unmappedTargetPolicy = org.mapstruct.ReportingPolicy.IGNORE)
public abstract class ResourceMapper extends EntityMapper<Resource, Resource, ResourceVo>
        implements TreeMapper<String, Resource, ResourceVo> {
    @Override
    @Mapping(target = "creator", source = "creatorId", qualifiedByName = "getUserIdAndNamePairFromId")
    @Mapping(target = "updater", source = "updaterId", qualifiedByName = "getUserIdAndNamePairFromId")
    public abstract ResourceVo toVo(Resource resource);

    @Override
    public Resource toEntity(Resource resource) {
        return resource;
    }

    public Collection<ResourceVo> toTree(Collection<Resource> resources, Integer maxDeep) {
        return TreeMapper.super.toTree(resources, this::toVo, Identifiable::getId, Resource::getParentId, ResourceVo::setChild, maxDeep == null ? 0 : maxDeep);
    }
}
